/* 
 * ProfileTool
 * Copyright 2006-2008 Craig Box
 * Released under the Mozilla Public License v1.1
 * 
 * http://www.codeplex.com/ProfileTool
 */

using System;
using System.IO;

namespace ProfileTool
{
	/// <summary>
	/// A profile object consists of a name, size, type, status, modification
	/// date (the same as the User Profiles box in Windows XP) but also a SID
	/// and a pathname.
	/// </summary>
	public class Profile
	{
		
		private string username;
		public string Username 
		{
			get
			{
				return username;
			}
			set
			{
				username = value;
			}
		}


		private DateTime modified;
		public DateTime Modified
		{
			get
			{
				return modified;
			}
			set
			{
				modified = value;
			}
		}

		private string sid;
		public string SID
		{
			get
			{
				return sid;
			}
			set
			{
				sid = value;
			}
		}

		private string path;
		public string Path
		{
			get
			{
				return path;
			}
			set
			{
				path = value;
			}
		}

		private long size;
		public long Size
		{
			get
			{
				return size;
			}
			set
			{
				size = value;
			}
		}

		private string roamingPath;
		public string RoamingPath
		{
			get
			{
				return roamingPath;
			}
			set
			{
				roamingPath = value;
			}
		}

		private int state;
		public int State
		{
			get
			{
				return state;
			}
			set
			{
				state = value;
			}
		}

		private int userpreference;
		public int UserPreference
		{
			get
			{
				return userpreference;
			}
			set
			{
				userpreference = value;
			}
		}

		public enum USERPREFERENCE 
		{
			LOCAL,
			REMOTE
		}

		private bool isHiveLoaded;
		public bool IsHiveLoaded
		{
			get
			{
				return isHiveLoaded;
			}
			set
			{
				isHiveLoaded = value;
			}
		}


		public Profile(string inputSid)
		{
			sid = inputSid;
			try
			{
				username = SidTranslator.GetName(inputSid);
			} 
			catch (Exception ex)
			{
				Console.WriteLine(ex.ToString());
				username = "Account unknown";
			}
			
			// initially we have not loaded this hive into the registry to play with
			isHiveLoaded=false;
		}

		public static long DirSize(DirectoryInfo d)
		{
			/* 
					 * WISHLIST: possibly consider reading the key in 
					 * HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\ExcludeProfileDirs
					 * or the desktop.ini file in the profile, 
					 * and only acting based on that.  This is a good enough estimation.
					 */
			long Size = 0;
			// Add file sizes.
			FileInfo[] fis = d.GetFiles();
			foreach (FileInfo fi in fis)
			{
				try
				{
					Size += fi.Length;
				}
				catch (FileNotFoundException)
				{
					// counting sizes - ignore and move on.
					continue;
				}
			}
			// Add subdirectory sizes.
			DirectoryInfo[] dis = d.GetDirectories();
			foreach (DirectoryInfo di in dis)
			{
				// profile count doesn't include Local Settings dir in the MS capplet, so 
				// it does't here either.
				if (di.Name != "Local Settings")
				{
					Size += DirSize(di);
				}
			}
			return (Size);
		}

	}
}



